/**
 * Project: TacticaPescaditos
 * File: ReceiveBall.java
 */
package org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.fsm.state;

import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.SteeringBehaviors;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.TacticaPescaditos;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.TeamPlayer;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.geometrics.Vector2D;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.MessageDispatcher;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.Telegram;

/**
 * @author Carlos Alberto
 * class: ReceiveBall
 */
public class ReceiveBall implements State<TeamPlayer> {
	
	private String name;
	
	/**
	 * 
	 */
	private ReceiveBall() {
		this.name = "ReceiveBall";
	}

	public static final State<TeamPlayer> instance = new ReceiveBall();

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#enter(java.lang.Object)
	 */
	public void enter(TeamPlayer player) {  
		TacticaPescaditos tactica = player.getTactica();
		tactica.setReceiver(player);
		tactica.setControllingPlayer(player);




		if ( player.inHotRegion() ||
				tactica.isOpponentWithinRadius(player.getPosition(),TeamPlayer.RECEIVING_RANGE)) {			
			player.getSteering().seekOn();
		} else {
			player.getSteering().pursuitOn();
		}
		
		MessageDispatcher dispatcher = tactica.getDispatcher();
		dispatcher.dispatch(player.getId(), player.getId(), Telegram.MessageType.RECALCULATE_RECEIVING_TARGET, null, 1);
	}

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#execute(java.lang.Object)
	 */
	public void execute(TeamPlayer player) {
		TacticaPescaditos tactica = player.getTactica();
		
		if (player.canKick() &&  tactica.isControlling()) {
			 player.getStateMachine().changeState(KickBall.instance);
			    return;	
		}
		
		if (player.ballWithinReceivingRange()) {
			player.getSteering().seekOn();
			player.getSteering().setTarget(player.getBall().getPosition());
			return;
		}
		
		 if (tactica.getReceiver() != player  ) {
			
			 player.getStateMachine().changeState(Wait.instance);
		    return;
		  }  
		 
		  if (player.getSteering().isPursuitOn() || player.getBall().getSpeed()<1) {
			  player.getSteering().setTarget(player.getBall().getPosition());
		  }	

		  if (player.isAtTarget()) {
			  player.getSteering().seekOff();
			  player.getSteering().pursuitOff();
			  player.setVelocity(new Vector2D(0,0));
			  if (tactica.isOpponentWithinRadius(player.getBall().getPosition(),TeamPlayer.RECEIVING_RANGE)) {
				  player.getSteering().seekOn();
				  player.getSteering().pursuitOff();
				  player.getSteering().setTarget(tactica.getBall().getPosition());
			  }
			  
		  } 		  







	}

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#exit(java.lang.Object)
	 */
	public void exit(TeamPlayer player) {
		SteeringBehaviors steering = player.getSteering();
		steering.seekOff();
		steering.pursuitOff();
		player.getTactica().setReceiver(null);
	}

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#onMessage(java.lang.Object, com.chidotronics.javacup.fsm.state.Telegram)
	 */
	public boolean onMessage(TeamPlayer o, Telegram telegram) {
		if (Telegram.MessageType.RECALCULATE_RECEIVING_TARGET.equals(telegram.getMessageType())){
			
			return true;
		}
		return false;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return this.name.hashCode();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return this.name;
	}
}
